package com.hkrt.demo.demo1.dao;
/**
 * DAO:DataBase Access Object
 *
 */

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;



public class BaseDao {
	private String url = "jdbc:mysql://127.0.0.1:3306/test";
	private String driver = "com.jdbc.mysql.Driver";
	private String username = "root";
	private String password = "root";
	private Connection conn;
	private PreparedStatement pst;
	protected ResultSet rst;
	
	/**
	 * lianjie
	 */
	protected Connection getConnection() {
		if (conn == null) {
			try {
				Class.forName(driver);
				conn = DriverManager.getConnection(url, username, password);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return conn;
	}
	
	/**
	 * update
	 * insert into table()values(?,?,?)
	 */
	protected int update(String sql,Object... params) {
		System.out.println(">>>>==="+sql);
		System.out.println(">>>>=Param:="+Arrays.toString(params));
		getConnection();
		
		try {
			pst = conn.prepareStatement(sql);
		

			if(params!=null) {
				for (int i = 0; i < params.length; i++) {
					pst.setObject(i+1, params[i]);
				}
			}
			
			int rows = pst.executeUpdate();
		
			return rows;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			close();
		}
		
		return -1;
	}
	/**
	 * chazhao
	 */
	protected ResultSet query(String sql,Object... params) {
		System.out.println(">>>>>=="+sql);
		System.out.println(">>>>params:"+Arrays.toString(params));
		getConnection();
		
		try {
			pst = conn.prepareStatement(sql);
			
			if(params!=null&&params.length>0) {
				for (int i = 0; i < params.length; i++) {
					pst.setObject(i+1, params[i]);
				}
			}
			
			return pst.executeQuery();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			/////
		}
		
		
		return null;
	}
	
	/**
	 * select name,price from product
	 * 
	 */
	public <T> List<T> query(String sql,Class<T> cls,Object... params){
		rst = query(sql, params);
		List<T> list = new ArrayList<T>();
		//ʹ�÷��似�� ��̬��װ
		try {
			ResultSetMetaData rsmd = rst.getMetaData();
			//ȡ������
			int count = rsmd.getColumnCount();
			
			while(rst.next()) {
				//ʵ��������
				T t = (T)cls.newInstance();
				//��������ѭ�� ȡ��ÿһ�е����� 
				for (int i = 0; i < count; i++) {
					//price  ȡ������
					String colsName = rsmd.getColumnLabel(i+1);
					//��ǰ�е�ֵ
					Object colsValue = rst.getObject(colsName);
					
//					System.out.print(colsValue+" ");
					//�����setter����----setPrice(float)
					
					//��ȡ����
					Field field = t.getClass().getDeclaredField(colsName);
					
					String setName = "set"+colsName.substring(0,1).toUpperCase()+colsName.substring(1);
					Method setMethod = t.getClass().getDeclaredMethod(setName, field.getType());
					//ִ��setter�����������Ը�ֵ
					setMethod.invoke(t, colsValue);
				}
				//�ѷ�װ�Ķ������list
				list.add(t);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return list;
	}
	

	/**
	 * guanbi
	 */
	protected void close() {
		if (rst != null) {
			try {
				rst.close();
				rst = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (pst != null) {
			try {
				pst.close();
				pst = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
				conn = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
